package com.carbon.trade.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carbon.common.api.Paging;
import com.carbon.trade.entity.CarbonTradeQuote;
import com.carbon.trade.mapper.CarbonTradeQuoteMapper;
import com.carbon.trade.param.CarbonTradeQuoteQueryParam;
import com.carbon.trade.param.StartTradingParam;
import com.carbon.trade.service.CarbonTradeQuoteService;
import com.carbon.trade.vo.CarbonTradeQuoteQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CarbonTradeQuoteServiceImpl extends ServiceImpl<CarbonTradeQuoteMapper, CarbonTradeQuote> implements CarbonTradeQuoteService {

    @Override
    public Paging<CarbonTradeQuoteQueryVo> getPageList(CarbonTradeQuoteQueryParam queryParam) {
        // 创建分页对象
        Page<CarbonTradeQuote> page = new Page<>(queryParam.getCurrent(), queryParam.getSize());
        
        // 构建查询条件
        QueryWrapper<CarbonTradeQuote> queryWrapper = new QueryWrapper<>();
        
        // 根据交易角色过滤
        if (queryParam.getTradeRole() != null && !queryParam.getTradeRole().isEmpty()) {
            queryWrapper.eq("trade_role", queryParam.getTradeRole());
        }
        
        // 根据资产类型过滤
        if (queryParam.getAssetType() != null && !queryParam.getAssetType().isEmpty()) {
            queryWrapper.eq("asset_type", queryParam.getAssetType());
        }
        
        // 根据项目类型过滤
        if (queryParam.getProjectType() != null && !queryParam.getProjectType().isEmpty()) {
            queryWrapper.eq("project_type", queryParam.getProjectType());
        }
        
        // 根据项目范围过滤
        if (queryParam.getProjectScope() != null && !queryParam.getProjectScope().isEmpty()) {
            queryWrapper.eq("project_scope", queryParam.getProjectScope());
        }
        
        // 根据过期时间范围过滤
        if (queryParam.getExpirationDateStart() != null) {
            queryWrapper.ge("expiration_date", queryParam.getExpirationDateStart());
        }
        if (queryParam.getExpirationDateEnd() != null) {
            queryWrapper.le("expiration_date", queryParam.getExpirationDateEnd());
        }
        
        // 根据关键词搜索
        if (queryParam.getKeyword() != null && !queryParam.getKeyword().isEmpty()) {
            queryWrapper.and(wrapper -> wrapper
                .like("institution_name", queryParam.getKeyword())
                .or()
                .like("project_name", queryParam.getKeyword())
            );
        }
        
        // 按创建时间倒序排列
        queryWrapper.orderByDesc("created_time");
        
        // 执行分页查询
        IPage<CarbonTradeQuote> result = baseMapper.selectPage(page, queryWrapper);
        
        // 转换为VO对象
        List<CarbonTradeQuoteQueryVo> voList = new ArrayList<>();
        for (CarbonTradeQuote entity : result.getRecords()) {
            CarbonTradeQuoteQueryVo vo = convertToVo(entity);
            voList.add(vo);
        }
        
        // 返回分页结果
        return new Paging<>(result.getTotal(), result.getCurrent(), voList);
    }

    @Override
    public boolean add(CarbonTradeQuote carbonTradeQuote) {
        return baseMapper.insert(carbonTradeQuote) > 0;
    }

    @Override
    public boolean startTrading(StartTradingParam param) {
        // TODO: 实现开始交易逻辑
        return true;
    }

    @Override
    public Paging<CarbonTradeQuoteQueryVo> search(String keyword, int current, int size) {
        CarbonTradeQuoteQueryParam queryParam = new CarbonTradeQuoteQueryParam();
        queryParam.setKeyword(keyword);
        queryParam.setCurrent(current);
        queryParam.setSize(size);
        return getPageList(queryParam);
    }

    /**
     * 将实体对象转换为VO对象
     */
    private CarbonTradeQuoteQueryVo convertToVo(CarbonTradeQuote entity) {
        CarbonTradeQuoteQueryVo vo = new CarbonTradeQuoteQueryVo();
        BeanUtils.copyProperties(entity, vo);
        
        // 设置资产类型名称
        vo.setAssetTypeName(getDictName("asset_type", entity.getAssetType()));
        
        return vo;
    }

    /**
     * 获取字典名称（简化实现）
     */
    private String getDictName(String dictType, String dictValue) {
        // TODO: 实现字典查询逻辑
        return dictValue;
    }
}